package com.eplayer.mqdx.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.PageTransformer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.eplayer.mqdx.R;

public class GuideActivity extends Activity {
	ViewPager viewPager;
	ArrayList<View> lists = new ArrayList<View>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_guide);
        
		viewPager = (ViewPager) findViewById(R.id.pager);
		initDate();
		viewPager.setPageTransformer(true, new MyPageTransformer());
		MyPagerAdapter adapter = new MyPagerAdapter();
		viewPager.setAdapter(adapter);

	}
	private final class MyPageTransformer implements PageTransformer {

		private static final float MIN_SCALE = 0.85f;
		private static final float MIN_ALPHA = 0.5f;

		@Override
		public void transformPage(View view, float position) {
			int pageWidth = view.getWidth();
			int pageHeight = view.getHeight();
			if (position < -1) {
				view.setAlpha(0);
			} else if (position <= 1) {
				float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
				float vertMargin = pageHeight * (1 - scaleFactor) / 2;
				float horzMargin = pageWidth * (1 - scaleFactor) / 2;
				if (position < 0) {
					view.setTranslationX(horzMargin - vertMargin / 2);
				} else {
					view.setTranslationX(-horzMargin + vertMargin / 2);
				}
				view.setScaleX(scaleFactor);
				view.setScaleY(scaleFactor);
				view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE)
						/ (1 - MIN_SCALE) * (1 - MIN_ALPHA));
			}else {
				view.setAlpha(0);
			}

		}
	}

	private void initDate() {
		View guide1 = View.inflate(this, R.layout.viewpager_a, null);
		View guide2 = View.inflate(this, R.layout.viewpager_b, null);
		View guide3 = View.inflate(this, R.layout.viewpager_c, null);
		Button bt_start = (Button) guide3.findViewById(R.id.bt_start);
		bt_start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GuideActivity.this,
						MainActivity.class);
				startActivity(intent);
				finish();
			}
		});
		lists.add(guide1);
		lists.add(guide2);
		lists.add(guide3);
	}

	class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			View view = lists.get(position);
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// super.destroyItem(container, position, object);
			container.removeView((View) object);
		}

	}
}
